package flib;

import java.io.PrintWriter;

import processing.core.PApplet;

public class Index {
boolean[][]index=new boolean[16][16];
float w;
float h;
float area;
float proportion;
float angle;
float distance;

float[] distValues=new float[4];

Object indexed;

public Index(boolean[][]index,float w,float h){
	this.index=index;
	this.w=w;
	this.h=h;
	this.area=w*h;
	this.proportion=w/h;
}
public Index(PApplet applet, String path){
	load(applet,path);
}
public void save(PApplet applet, String path){
	PrintWriter writer=applet.createWriter(path);
	String shape=new String();
	shape+="s 16";
	for (int x=0;x<index.length;x++){
		for (int y=0;y<index[x].length;y++){
		if (index[x][y]){
			shape+=" 1";
		}
		else{
			shape+=" 0";
		}
		}
	}
	writer.println(shape);
	writer.println("dim "+w+" "+h);
	writer.flush();
	writer.close();
}
public void load(PApplet applet, String path){
	String[] strings=applet.loadStrings(path);
	for (int i=0;i<strings.length;i++){
		String s=strings[i];
		String[] segs=s.split(" ");
		String start=segs[0];
		if (start.equals("s")){
			int n=Integer.valueOf(segs[1]);
			index=new boolean[n][n];
			int cI=2;
			for (int x=0;x<n;x++){
				for (int y=0;y<n;y++){
					int v=Integer.valueOf(segs[cI]);
					index[x][y]=v==1;
					cI++;
				}
			}
		}
		else if (start.equals("dim")){
			w=Float.valueOf(segs[1]);
			h=Float.valueOf(segs[2]);
			area=w*h;
			proportion=w/h;
		}
	}
}


}
